export default class MBase {
  /**去除字符串两端指定字符 */
  public static trimStr(str: string, lm: string = "") {
    let sps: Array<string> = [" "].concat(lm.split(""));
    sps = sps.map((x) => x.replace(/\\/, "\\\\"));
    sps.forEach((x) => {
      str = str.replace(new RegExp(`^${x}`, "g"), "");
      str = str.replace(new RegExp(`${x}$`, "g"), "");
    });
    return str;
  }

  /** 获取分隔后的最后一节*/
  public static lastWord(text?: string, sp: string = "/"): string {
    if (!text) return "";
    return text.split(sp).pop() || text;
  }

  /**设置缩进 */
  public static ident(lines: Array<string>, count: number = 0) {
    count = parseInt(`${count}`) || 0;
    if (count < 0) {
      const reg = new RegExp(`^\\s{${Math.abs(count)}}`);
      return lines.map((x) => x.replace(reg, ""));
    } else if (count > 0) {
      const space = new Array(count).fill(" ").join("");
      return lines.map((x) => `${space}${x}`);
    } else {
      return lines;
    }
  }

  /**转成多行 */
  public static toRows(lines: Array<string>) {
    return lines.join("\r\n");
  }

  /**转成类C注释 */
  public static toCNote(text: Array<string> | string, ident: number = 0) {
    if (text instanceof Array) {
      return MBase.ident([`/**`, ...text.map((x) => ` * ${x}`), ` */`], ident);
    } else {
      return MBase.ident([`/** ${text} */`], ident);
    }
  }

  /**转成通用注释 */
  public static toCommonNote(text: Array<string> | string, ident: number = 0) {
    const txt = text instanceof Array ? text : [`${text || ""}`];
    const txts = txt.map((x) => `// ${x}`);
    return MBase.ident(txts, ident);
  }

  /**转成dart注释 */
  public static toDartNote(text: Array<string> | string, ident: number = 0) {
    const txt = text instanceof Array ? text : [`${text || ""}`];
    const txts = txt.map((x) => `/// ${x}`);
    return MBase.ident(txts, ident);
  }

  /**对象解析成字符串 */
  public static parseObj(obj: Record<string, string>, sp: string = " ") {
    let res: Array<string> = [];
    for (let p in obj) res.push(`${p}=${obj[p]}`);
    return res.join(sp);
  }

  /**首字母大写 */
  public static camelUpperCase(text?: string) {
    return text ? `${text.charAt(0).toUpperCase()}${text.slice(1)}` : "";
  }

  /**首字母小写 */
  public static camelLowercase(text?: string) {
    return text ? `${text.charAt(0).toLowerCase()}${text.slice(1)}` : "";
  }

  /**驼峰命名法 */
  public static camelCaseName(txts: Array<string>, lower: boolean = false) {
    if (lower) {
      const words = txts.slice(0);
      return [MBase.camelLowercase(words.shift())]
        .concat(words.map((w) => MBase.camelUpperCase(w)))
        .join("");
    } else {
      return txts.map((w) => MBase.camelUpperCase(w)).join("");
    }
  }

  /**蛇形命名法 */
  public static snakeCaseName(txts: Array<string>, sp: string = "_") {
    return txts.map((x) => MBase.camelLowercase(x)).join(sp);
  }

  /**蛇形命名法转驼峰命名法 */
  public static snakeToCamel(txt: string, lower: boolean = false) {
    const words = txt.split("-").map(x=>x.split("_")).flat();
    return MBase.camelCaseName(words, lower);
  }

  /**驼峰命名法转蛇形命名法 */
  public static camelToSnake(txt: string, sp: string = "_") {
    const words = txt.split(/(?=[A-Z])/g);
    return words.map((x) => MBase.camelLowercase(x)).join(sp);
  }
}
